function showTableData(){

}


function getTableData(){
    // 清除指定元素的内容
    document.getElementById('info-result-tbody').innerHTML = '';
    document.getElementById("search-result-info").style.display="block";
    var userIDText = document.getElementById("h2-userID").textContent;
    var userID = userIDText.replace("用户","").replace("查询结果","");
    var form1=document.getElementById("info-search");
    var year = form1.elements['leftDropdown'].value;
    var time = form1.elements['rightDropdown'].value;
    fetch('pftInfo', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            userID:userID,
            year: year,
            time: time
        })
    })
        .then(response => response.json())
        .then(datalist => {
            console.log(datalist);
            displayInfo(datalist);
            console.log("success");
        })
        .catch(error => {
            console.error('There has been a problem with your fetch operation:', error);
        });
}

let stringList1 = ["周一","周二","周三","周四","周五","周六","周日"];
let stringList2 = ["（1，2）","（3，4）","（5，6）","（7，8）","（9，10）","（11，12）"];

var rows = 7;
var columns = 6;
var myArray = [];

// 嵌套循环创建二维数组
for (var i = 0; i < rows; i++) {
    // 初始化每一行
    var rowArray = [];

    for (var j = 0; j < columns; j++) {
        // 在每一行中添加元素（可以是任何值，这里以行列索引为例）
        rowArray.push(0);
    }

    // 将每一行添加到二维数组中
    myArray.push(rowArray);
}

function selected(button){
    button.classList.toggle("stu-selected");
    let buttonID = button.id;
    let ID = buttonID.replace("stu-select-but","");
    let selected = document.getElementById("stu-select-sel"+ID);
    let date = document.getElementById("stu-select-date"+ID).innerText;
    let text = button.textContent;
    console.log(ID);
    let text1 = selected.textContent;
    let num = parseInt(text1);
    if(text==="选课"){
        // 判断data里是否包含两个列表的某一项，找出对应项，使得myArray对应的数据为1
        for (let i = 0; i < stringList1.length; i++) {
            for (let j = 0; j < stringList2.length; j++) {
                let searchString = stringList1[i] + stringList2[j];

                if (date.includes(searchString)) {
                    // 在myArray中对应位置设置为1
                    if(myArray[i][j] ===0){
                        myArray[i][j] = parseInt(ID);
                        console.log(myArray[i][j]);
                        button.innerText = "退课";
                        num+=1
                        selected.innerText = num.toString();
                    }
                    else {
                        alert("课程冲突!");
                    }
                }
            }
        }

    }
    else{
        // 判断data里是否包含两个列表的某一项，找出对应项，使得myArray对应的数据为1
        for (let i = 0; i < stringList1.length; i++) {
            for (let j = 0; j < stringList2.length; j++) {
                let searchString = stringList1[i] + stringList2[j];

                if (date.includes(searchString)) {
                    // 在myArray中对应位置设置为1
                    myArray[i][j] = 0;
                }
            }
        }
        button.innerText = "选课";
        num-=1
        selected.innerText = num.toString();
    }
}

function subSelect(){
    let list = "";
    for(var i=0;i<myArray.length;i++){
        for(var j=0;j<myArray[i].length;j++){
            if(myArray[i][j]!==0){
                list+=myArray[i][j].toString()+",";
            }
        }
    }
    let userID = document.getElementById("person-img").title;
    fetch('/processArray', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            userID:userID,
            list:list
        })
    })
        .then(response => response.text())
        .then(data => {
            console.log('Response from server:', data);
        })
        .catch(error => {
            console.error('Error:', error);
        });
}